package com.code.day22;

import org.junit.Test;

import java.lang.reflect.Field;

public class Demo03Filed {
    @Test
    public  void mehtod1() {
        Class<Person> personClass = Person.class;
        Field[] fields = personClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

    @Test
    public void method02(){
        Class<Person> personClass = Person.class;
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
    }
    @Test
    public void method03() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        Field sex = personClass.getField("sex");
        sex.set(person,"男");
        System.out.println(sex.get(person));
    }

    @Test
    public void method04() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        Field name = personClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(person,"刘大胆");
        System.out.println(name.get(person));
    }
}
